package com.monika.main.system.mq;

import cn.hutool.core.thread.NamedThreadFactory;
import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:whh
 * @date: 2024-12-04 20:33
 * <p></p>
 */

@Configuration
public class RingBufferConfig {



    @Bean
    public RingBuffer<MsgEvent> ringBuffer(){

        NamedThreadFactory threadFactory = new NamedThreadFactory("MsgEvent-",true);
        EventFactory<MsgEvent> eventFactory = new EventFactory<MsgEvent>() {
            @Override
            public MsgEvent newInstance() {
                return new MsgEvent();
            }
        };


        Disruptor<MsgEvent> disruptor = new Disruptor(eventFactory,1024, threadFactory);


        MsgEventHandler m1 = new MsgEventHandler("m1");
        MsgEventHandler m2 = new MsgEventHandler("m2");

        //disruptor.handleEventsWith(m1,m2); //统一消费：一个消息会被所有消费者消费

        disruptor.handleEventsWithWorkerPool(m1,m2);//分组消费：一个消息只能被一个消费者消费，多消费者轮询处理

        //disruptor.handleEventsWith(m1).then(m2);   //顺序消费：1、3先并行处理，然后2再处理

        disruptor.start();
        return disruptor.getRingBuffer();
    }
}
